package com.demo.studentmanager;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.demo.studentmanager.databinding.ActivityRegisterBinding;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityRegisterBinding binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = binding.etAccount.getText().toString();
                String clazz = binding.etClass.getText().toString();
                String phone = binding.etPhone.getText().toString();
                String password = binding.etPassword.getText().toString();
                String passwordSure = binding.etPasswordSure.getText().toString();
                String sid = binding.etSid.getText().toString();
                if (name.isEmpty() || clazz.isEmpty() || phone.isEmpty() || password.isEmpty() || passwordSure.isEmpty()|| sid.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请完成所有内容！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.equals(passwordSure)) {
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }
                User user = new User();
                user.setName(name);
                user.setClazz(clazz);
                user.setAccount(phone);
                user.setPassword(password);
                user.setSid(sid);
                if (new DBHelper(RegisterActivity.this).register(user)) {
                    Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(RegisterActivity.this, "用户名已被注册！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        binding.tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void back(View view) {
        finish();
    }
}